import { getDiffDays, returnCompareTime } from "@/libs/util.js";
import Vue from "vue";
export default {
  data () {
    return {};
  },
  methods: {
    // 选择收货天数计算收发货日期
    receivingDays (val) {
      if (!this.form.fromTime && !this.form.toTime) return;
      const { billDateFlag, sameMonthDay, lastMonthDay } = this.systemBillObj;
      let sameMonthDayFormat = new Date(this.$moment(sameMonthDay).format("YYYY-MM-DD HH:mm:ss"));
      let lastMonthDayFormat = new Date(this.$moment(lastMonthDay).format("YYYY-MM-DD HH:mm:ss"));

      if (this.form.fromTime) {
        this.form.toTime = this.$moment(this.form.fromTime)
          .add(val, "days")
          .format("YYYY-MM-DD 23:59");
        // 已过关账期 时效天数校验
        if (billDateFlag && returnCompareTime(sameMonthDayFormat, new Date(this.$moment(this.form.toTime).format("YYYY-MM-DD HH:mm:ss"))
        )) {
          this.form.fromTime = null;
          this.form.toTime = null;
          this.form.days = null;
          Vue.prototype.$message({ type: "warning", message: "关账期原因，收发货时间只能选择当前月及之后的时间" });
        }
        if (!billDateFlag && returnCompareTime(lastMonthDayFormat, new Date(this.$moment(this.form.toTime).format("YYYY-MM-DD HH:mm:ss"))
        )) {
          this.form.fromTime = null;
          this.form.toTime = null;
          this.form.days = null;
          Vue.prototype.$message({ type: "warning", message: "关账期原因，收发货时间只能选择上一月及之后的时间" });
        }
      }
      if (this.form.toTime) {
        if (this.form.fromTime) {
          let oldFromTime = this.$moment(this.form.fromTime).format(
            "YYYY-MM-DD HH:mm"
          );
          let time = oldFromTime.substring(11, 16);
          this.form.fromTime = this.$moment(this.form.toTime)
            .subtract(val, "days")
            .format(`YYYY-MM-DD ${time}`);
        } else {
          this.form.fromTime = this.$moment(this.form.toTime)
            .subtract(val, "days")
            .format("YYYY-MM-DD 00:00");
        }

        // 已过关账期，发货时间要等于晚于关账期当月1号
        let fromTimeFormat = new Date(this.$moment(this.form.fromTime).format("YYYY-MM-DD HH:mm:ss"));
        let fromTimeAfter = sameMonthDayFormat - fromTimeFormat > 0;
        let toTimeAfter = lastMonthDayFormat - fromTimeFormat > 0;

        if (billDateFlag && fromTimeAfter) {
          this.form.fromTime = null;
          this.form.days = null;
          Vue.prototype.$message({ type: "warning", message: "关账期原因，收发货时间只能选择当前月及之后的时间" });
        }
        if (!billDateFlag && toTimeAfter) {
          this.form.fromTime = null;
          this.form.days = null;
          Vue.prototype.$message({ type: "warning", message: "关账期原因，收发货时间只能选择上一月及之后的时间" });
        }
      }

      this.vaild();
    },
    // 修改时间计算天数和收货日期(type=0发货时间，1收货时间)
    changeTime (val, type) {
      if (val) {
        let value = this.$moment(val).format("YYYY-MM-DD HH:mm");
        if (type) {
          if (
            this.form.fromTime &&
            (new Date(val).getTime() < new Date(this.form.fromTime).getTime() ||
              value ===
              this.$moment(this.form.fromTime).format("YYYY-MM-DD HH:mm"))
          ) {
            this.form.toTime = this.$moment(val).format(
              `YYYY-MM-DD ${this.pickerOptions.selectableRange.substring(0, 5)}`
            );
          }
          let tMat = this.$moment(val).format("YYYY-MM-DD HH:mm");
          if (tMat.substring(11, 16) === '00:00') {
            this.form.toTime = this.$moment(val).format("YYYY-MM-DD 23:59");
          }
          if (!this.form.fromTime && this.form.days) {
            this.form.fromTime = this.$moment(this.form.toTime)
              .subtract(this.form.days, "days")
              .format("YYYY-MM-DD 00:00");
          }
        } else {
          if (
            this.form.toTime &&
            (new Date(val).getTime() > new Date(this.form.toTime).getTime() ||
              value ===
              this.$moment(this.form.toTime).format("YYYY-MM-DD HH:mm"))
          ) {
            this.form.toTime = "";
            this.form.days = null;
          }
          if (
            this.form.flyTime &&
            (new Date(val).getTime() > new Date(this.form.flyTime).getTime() ||
              value ===
              this.$moment(this.form.flyTime).format("YYYY-MM-DD HH:mm"))
          ) {
            this.form.flyTime = "";
          }
          if (
            this.form.trainTime &&
            (new Date(val).getTime() >
              new Date(this.form.trainTime).getTime() ||
              value ===
              this.$moment(this.form.trainTime).format("YYYY-MM-DD HH:mm"))
          ) {
            this.form.trainTime = "";
          }
          if (
            this.form.toTime &&
            (new Date(val).getTime() > new Date(this.form.toTime).getTime() ||
              value ===
              this.$moment(this.form.toTime).format("YYYY-MM-DD HH:mm"))
          ) {
            this.form.toTime = `${this.form.toTime.substring(
              0,
              10
            )} ${this.pickerOptions.selectableRange.substring(0, 5)}`;
          }

          if (this.form.days || this.form.days === 0) {
            if (this.form.toTime) {
              let oldToTime = this.$moment(this.form.toTime)
                .format("YYYY-MM-DD HH:mm")
                .substring(11, 16);
              this.form.toTime = this.$moment(val)
                .add(this.form.days, "days")
                .format(`YYYY-MM-DD ${oldToTime}`);
            } else {
              this.form.toTime = this.$moment(val)
                .add(this.form.days, "days")
                .format("YYYY-MM-DD 23:59");
            }
          }
        }
      } else {
        this.form.days = null;
        if (type) {
          this.form.toTime = "";
        } else {
          this.form.fromTime = "";
        }
      }
      this.vaild();
    },
    handlerChangeTime (val) {
      if (val.fromTime) {
        // 所选择发货时间的时分为23:59, fromTimeFlag为true，则屏蔽时效天数中的-当天-选项
        // if (
        //   this.$moment(val.fromTime)
        //     .format("YYYY-MM-DD HH:mm")
        //     .substring(11, 16) === "23:59"
        // ) {
        //   this.form.fromTimeFlag = true;
        //   if (this.form.days === 0 || this.form.days === null) {
        //     // 当前的时效天数为-当天-，则清空时效天数、收货时间
        //     this.form.days = null;
        //     this.form.toTime = "";
        //     val.toTime = "";
        //     return;
        //   }
        // } else {
        //   this.form.fromTimeFlag = false;
        // }
        let toflag = returnCompareTime(
          new Date(this.$moment(val.toTime).format("YYYY-MM-DD HH:mm:ss")),
          new Date(this.$moment(val.fromTime).format("YYYY-MM-DD HH:mm:ss"))
        );
        let a =
          this.$moment(val.toTime).format("YYYY-MM-DD HH:mm:ss") !=
          this.$moment(val.fromTime).format("YYYY-MM-DD HH:mm:ss");
        if (!toflag && val.toTime != "" && a && val.days) {
          return (val.toTime = this.$moment(val.fromTime).format(
            "YYYY-MM-DD 23:59:59"
          ));
        }
        if (val.toTime && !a) {
          this.form.toTime = this.$moment(this.form.fromTime).format(
            "YYYY-MM-DD 23:59:59"
          );
        }

        let formDays = this.$moment(val.fromTime)
          .format("YYYY-MM-DD HH:mm")
          .substring(0, 10);
        let fromTimes = this.$moment(val.fromTime)
          .format("YYYY-MM-DD HH:mm")
          .substring(11, 16);
        let toDays = val.toTime
          ? this.$moment(val.toTime).format("YYYY-MM-DD HH:mm").substring(0, 10)
          : null;

        let hour = fromTimes.substring(0, 2);
        let minute = fromTimes.substring(3, 5);
        if (Number(minute) != 59) {
          minute =
            minute[0] == 0 ? `0${Number(minute) + 1}` : Number(minute) + 1;
        } else {
          minute = Number(hour) + 1 == 24 ? "59" : "00";
          hour =
            hour[0] == 0 && hour[1] != 9
              ? `0${Number(hour) + 1}`
              : Number(hour) + 1 == 24
                ? "23"
                : Number(hour) + 1;
        }
        if (formDays != toDays) {
          this.pickerOptions.selectableRange = `00:00:00 - 23:59:59`;
        } else {
          this.pickerOptions.selectableRange = `${hour}:${minute}:59 - 23:59:59`;
        }
        if (val.toTime) {
          val.days = getDiffDays(
            this.$moment(val.fromTime).format("YYYY-MM-DD"),
            this.$moment(val.toTime).format("YYYY-MM-DD")
          );
        }
      }
    },
  }
};
